<?php
/**
 * Minhlog 文件缓存类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Cache
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: File.php 3 2010-01-17 14:03:54Z Minh.Shu $
 *
 */

class Minh_Cache_File
{
    protected $_cache;             //缓存对象
    private $_cacheStatus = true;  //缓存状态
    private $_lifeTime = 3600;     //默认缓存时间

    /**
     * 设置页面缓存对象
     * @return object
     */
    public function setCache ()
    {
        if(null === $this->_cache){
            $cacheDir = APPLICATION_PATH . '/caches';    //缓存目录
            if (! file_exists($cacheDir)) {
                @mkdir($cacheDir, 0777);
            }
            $frontendOptions = array('caching' => $this->_cacheStatus ,
                    'cache_id_prefix' => 'minhlog' ,
                    'lifeTime' => $this->_lifeTime ,
                    'automatic_serialization' => true ,
                    'automatic_cleaning_factor' => 1 ,
                    'ignore_user_abort' => true
            );
            $backendOptions = array('cache_dir' => $cacheDir);
            $this->_cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);
        }

        return $this->_cache;
    }

    /**
     * 获取页面缓存对象
     * @return object
     */
    public function getCache ()
    {
        return $this->setCache();
    }

    /**
     * 设置缓存开关
     * @param boolean $status
     * @return object
     */
    public function setCacheStatus ($status = true)
    {
        $this->_cacheStatus = (boolean) $status;
        return $this;
    }

    /**
     * 设置缓存时间方法
     * @param integer $seconds
     * @return object
     */
    public function setCacheLifeTime ($seconds = 600)
    {
        $this->_lifeTime = (int) $seconds;
        return $this;
    }
}
